package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductVo;
import cn.itcast.vo.ExportResult;
import cn.itcast.vo.ExportVo;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController{

    @Reference
    private ContractService contractService;

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;


    //查询所有购销合同  状态 = 1
    //分页查询
    @RequestMapping("/contractList")
    public String contractList(@RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int size){
        ContractExample example = new ContractExample();
        //根据状态和企业id查询
        ContractExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        criteria.andStateEqualTo(1);

        PageInfo info = contractService.findAll(example,page,size);
        request.setAttribute("page",info);

        return "cargo/export/export-contractList";
    }

    //分页查询所有报运单
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int size){
        ExportExample example = new ExportExample();
        //根据企业id查询
        ExportExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        PageInfo info = exportService.findAll(example,page,size);
        request.setAttribute("page",info);
        return "cargo/export/export-list";
    }

    /**
     * 进入到添加出口报运单页面
     *      同名参数：
     *              string字符串
     *              string数组
     */
    @RequestMapping("/toExport")
    public String toExport(String id){
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }

    /**
     * 进入修改的页面
     */
    @RequestMapping("toUpdate")
    public String toUpdate(String id){
        //1.根据id 查询报运单信息
        Export export = exportService.findById(id);
        request.setAttribute("export",export);
        //2.根据报运单id，查询此报运单的所有商品
        ExportProductExample example = new ExportProductExample();
        ExportProductExample.Criteria criteria = example.createCriteria();
        criteria.andExportIdEqualTo(id);

        List<ExportProduct> list = exportProductService.findAll(example);
        request.setAttribute("eps",list);

        return "/cargo/export/export-update";
    }

    /**
     *  保存或修改
     */
     @RequestMapping("/edit")
     public String edit(Export export){
         export.setCompanyId(getLoginCompanyId());
         export.setCompanyName(getLoginCompanyName());
         if (StringUtils.isEmpty(export.getId())){
             exportService.save(export);
         }else{
             exportService.update(export);
         }
         return "redirect:/cargo/export/list.do";
     }

    /**
     *  海关电子报运
     *      参数：报运单id
     */
  @RequestMapping("/exportE")
    public String exportE(String id)  {
        //1.根据id查询报运单对象
        Export export = exportService.findById(id);
        //2.根据报运单id查询所有的商品数据
        ExportProductExample example = new ExportProductExample();
        ExportProductExample.Criteria criteria = example.createCriteria();
        criteria.andExportIdEqualTo(id);
        List<ExportProduct> eps = exportProductService.findAll(example);

        //3.将查询的报运数据转化为海关报运的vo对象
        //3.1 将Export对象转化为ExportVo对象
        ExportVo evo = new ExportVo();
        BeanUtils.copyProperties(export,evo); //源对象，目标对象
        evo.setExportId(export.getId());
        //3.2 saas中的ExportProduct转化为ExportProductVo对象
        List<ExportProductVo> list = new ArrayList<ExportProductVo>();
        for (ExportProduct ep : eps) {
            ExportProductVo epvo = new ExportProductVo();
            BeanUtils.copyProperties(ep,epvo);
            epvo.setExportProductId(ep.getId());
            epvo.setEid(id);
            list.add(epvo);
        }
        evo.setProducts(list); //报运单对象中包含所有的报运单商品
        //4.通过WebClient工作类发送报运数据
        WebClient webClient = WebClient.create("http://localhost:9090/ws/export/user");
        webClient.post(evo);
        //5.通过WebClient工具类查询报运结果
        WebClient client = WebClient.create("http://localhost:9090/ws/export/user/"+id);
        ExportResult result = client.get(ExportResult.class);
        //6.更新报运结果
       exportService.exportE(result);
        return "redirect:/cargo/export/list.do";
    }



    //货运管理 提交
    @RequestMapping("/submit")
    public String submit(String id){
      //将合同状态修改为 1 已上报
        Export export = new Export();
        export.setId(id);
        export.setState(1);
        exportService.update(export);
        return "redirect:/cargo/export/list.do";
    }

    //货运管理 取消
    @RequestMapping("/cancel")
    public String cancel(String id){
      //将合同状态修改为 0  草稿状态
       Export export = new Export();
       export.setId(id);
       export.setState(0);
       exportService.update(export);
        return "redirect:/cargo/export/list.do";
    }

    //查看货运管理
    @RequestMapping("/toView")
    public String toView(String id){
      //查询货物管理
        Export export = exportService.findById(id);
        request.setAttribute("export",export);
      return "/cargo/export/export-view";
    }


}
